CKEDITOR.dialog.add( 'splink', function( editor ) {
	var plugin = CKEDITOR.plugins.link;
	
	var urlRegex = /^((?:http|https|ftp|news):\/\/)?(.*)$/;
	
	var parseLink = function( editor, element ) {
			var href = ( element && ( element.data( 'cke-saved-href' ) || element.getAttribute( 'href' ) ) ) || '',
				urlMatch,
				retval = {};

			if ( href && ( urlMatch = href.match( urlRegex ) ) ) {
				retval.type = 'url';
				retval.url = {};
				retval.url.protocol = urlMatch[ 1 ];
				retval.url.url = urlMatch[ 2 ];
			} else
				retval.type = 'url';
			
			return retval;
		};

	var commonLang = editor.lang.common,
		linkLang = editor.lang.link;

	return {
		title: linkLang.title,
		minWidth: 350,
		minHeight: 50,
		contents: [{
			id: 'info',
			label: linkLang.info,
			title: linkLang.info,
			elements: [{
				type: 'text',
				id: 'url',
				label: commonLang.url,
				required: true,
				onLoad: function() {
					this.allowOnChange = true;
				},
				validate: function() {
					var dialog = this.getDialog();
					
					if ( ( /javascript\:/ ).test( this.getValue() ) ) {
						alert( commonLang.invalidValue );
						return false;
					}

					var func = CKEDITOR.dialog.validate.notEmpty( linkLang.noUrl );
					return func.apply( this );
				},
				setup: function( data ) {
					this.allowOnChange = false;
					if ( data.url )
						this.setValue( data.url.url );
					this.allowOnChange = true;
				},
				commit: function( data ) {
					if ( !data.url )
						data.url = {};

					data.url.url = this.getValue();
					this.allowOnChange = false;
				}
			}]
		}],
		onShow: function() {
			var editor = this.getParentEditor(),
				selection = editor.getSelection(),
				element = null;

			// Fill in all the relevant fields if there's already one link selected.
			if ( ( element = plugin.getSelectedLink( editor ) ) && element.hasAttribute( 'href' ) ) {
				// Don't change selection if some element is already selected.
				// For example - don't destroy fake selection.
				if ( !selection.getSelectedElement() )
					selection.selectElement( element );
			} else
				element = null;

			this.setupContent( parseLink.apply( this, [ editor, element ] ) );
		},
		onOk: function() {
			var attributes = {},
				data = {},
				editor = this.getParentEditor();

			this.commitContent( data );
			
			var url = ( data.url && CKEDITOR.tools.trim( data.url.url ) ) || '';
			attributes.href = ( url.indexOf( '/' ) === -1 ) ? 'http://' + url : url;

			var selection = editor.getSelection();
			
			var range = selection.getRanges()[ 0 ];
			
			// Use link URL as text with a collapsed cursor.
			if ( range.collapsed ) {
				// Short mailto link text view (#5736).
				var text = new CKEDITOR.dom.text( attributes.href, editor.document );
				range.insertNode( text );
				range.selectNodeContents( text );
			}
			
			// Apply style.
			var style = new CKEDITOR.style( { element: 'a', attributes: attributes } );
			style.type = CKEDITOR.STYLE_INLINE; // need to override... dunno why.
			style.applyToRange( range );
			range.select();
		},
		onFocus: function() {
			this.getContentElement( 'info', 'url' ).select();
		}
	};
} );
